package com.ruiyian.project.task;

import com.ruiyian.common.thread.ThreadPoolUtil;
import com.ruiyian.common.utils.DateUtils;
import com.ruiyian.common.utils.file.FileUtils;
import com.ruiyian.project.predict.service.PredictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author chong
 * @email 673426164@qq.com
 * @create 2022/3/30
 * Desc: 道路预测仿真定时任务
 */
@Slf4j
@Component("simTask")
public class Task {
    @Autowired
    private PredictService predictService;
    @Value("${simulation.writeDataPath}")
    private String writeDataPath;
    @Value("${simulation.writeMsgPath}")
    private String writeMsgPath;

    /**
     * 门架入口数据生成
     */
    public void creatRawData(){
        String endTime = DateUtils.getTime();
        String startTime = DateUtils.getNextDate(1, new Date(), -15, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String dateTime = DateUtils.getNextDate(1, new Date(), -15, new SimpleDateFormat("yyyyMMddHHmm"));
        log.info("任务开始执行------------------"+startTime+"--"+endTime);
        File file= new File(writeDataPath+"/"+dateTime);
        try {
            List<Map<String,String>> enterList = predictService.queryEntryData(startTime,endTime);
            List<Map<String,String>> exitList = predictService.queryExitData(startTime,endTime);
            List<Map<String,String>> gantryList = predictService.queryDoorFrameData(startTime,endTime);
            FileUtils.circleMethod(file,"ENTRY.dat",enterList);
            log.info("ENTRY.dat-----创建----------------数据个数"+enterList.size());
            FileUtils.circleMethod(file,"EXIT.dat",exitList);
            log.info("EXIT.dat-------创建---------------数据个数"+exitList.size());
            FileUtils.circleMethod(file,"GANTRY.dat",gantryList);
            log.info("GANTRY.dat------创建--------------数据个数-"+gantryList.size());
            List<String> msgList = msgData(startTime, endTime, dateTime);
            File fileMsg= new File(writeMsgPath);
            FileUtils.circleMsgMethod(fileMsg,"RawData"+dateTime+".msg",msgList);
            log.info("任务结束------------------------------"+dateTime);
            ThreadPoolUtil.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        if(enterList.size()>1){
                            enterList.remove(0);
                            predictService.batchAddEntry(enterList);
                        }
                    } catch (Exception e) {
                        log.error("添加入口数据异常"+e.getMessage());
                    }
                    try {
                        if(exitList.size()>1){
                            exitList.remove(0);
                            predictService.batchExitSave(exitList);
                        }
                    } catch (Exception e) {
                        log.error("添加出口数据异常"+e.getMessage());
                    }
                    try {
                        if(gantryList.size()>1){
                            gantryList.remove(0);
                            predictService.batchDoorSave(gantryList);
                        }
                    } catch (Exception e) {
                        log.error("添加门架数据异常"+e.getMessage());
                    }
                }
            });
        } catch (Exception e) {
            log.info("任务执行异常------------------------------"+e.getMessage()+e.getStackTrace()[0].getFileName()+e.getStackTrace()[0].getLineNumber());
        }
    }
    /**
     * 原始数据到达消息文件样例：
     * MSG_TYPE=raw_data
     * BEG_TIME=2020-07-28 12:30:00
     * END_TIME=2020-07-28 12:45:00
     * DATA_FOLDER=202007281230
     * @return
     */
    public List<String> msgData(String start,String endTime,String dataFol){
        List<String> list= new ArrayList<>();
        list.add("MSG_TYPE=raw_data");
        list.add("BEG_TIME="+start);
        list.add("END_TIME="+endTime);
        list.add("DATA_FOLDER="+dataFol);
        return list;
    }
}
